﻿using API_Service.DBHelper;
using Dapper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;

namespace API_Service.BizLogic.PartNo
{
    public class PartNoService : BaseLogicService
    {
        public PartNoService() { }

        public ExecutionResult<dynamic> GetPartNoByCode(string parameter)
        {
            var result = new ExecutionResult<dynamic>();
            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    string sql_partno_check = " select top 1 key_part_no from dbo.c_material_t  where key_part_no=@kpartno  ";

                    string sql_ssn_partno_check = " select top 1 key_part_no from dbo.r_kpn_sn_t  where kpn_sn=@ssn ";//table要更换

                    conn.Open();

                    result.Datas = conn.Query<dynamic>(sql_partno_check, new { kpartno = parameter }).ToArray();
                    if (result.Datas.Length == 0)
                    {//没有这个料号的信息
                        result.Datas = conn.Query<dynamic>(sql_ssn_partno_check, new { ssn = parameter }).ToArray();
                        if (result.Datas.Length > 0)
                        {//SSN与partno关系存在
                            result.Status = true;
                            result.Message = "KPSN";
                        }
                        else
                        {//ssn与partno关系不存在
                            result.Status = false;
                            result.Message = " 当前输入值无法正确转换料号，请确认输入值是否正确  ";
                        }
                    }
                    else
                    {//
                        result.Status = true;
                        result.Message = "KPN";
                    }                   
                }
                catch (Exception ex)
                {
                    Logger.WriteError(MethodBase.GetCurrentMethod(), ex);

                    result.Status = false;
                    result.Message = "Error: " + ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }

            return result;
        }

    }
}